package com.wtwd.campus.exception;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author ljiong
 * @date 2021/01/13
 * @description
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler
    public RespEntity exceptionHandler(Exception e){
        if(e instanceof BaseException){
            BaseException baseException=(BaseException)e;
            return new RespEntity(baseException.getRespCode());
        }else if(e instanceof ExcelImportException){
            ExcelImportException excelImportException = (ExcelImportException)e;
            return new RespEntity(excelImportException.getCode(),excelImportException.getMsg());
        }
        logger.error("------------捕获全局异常---------",e);
        e.printStackTrace();
        return new RespEntity(RespCode.ERROR);
    }
}
